import {showToast} from '../../utils/util';

Page({
  data: {
    address: {},
    totalPrice: 0,
    totalNum: 0,
    carts: []
  },
  onShow() {
    this.pageInit();
  },
  pageInit() {
    let address = wx.getStorageSync('address');
    let carts = wx.getStorageSync('carts');

    carts = carts.filter(v => v.is_select);

    let totalPrice = 0, totalNum = 0;

    carts.forEach(v => {
      if (v.is_select) {
        totalNum += v.num;
        totalPrice += v.num * v.goods_price;
      }
    })

    this.setData({
      address, carts,
      totalPrice, totalNum
    });
  },
  handlePayment: async function () {
    await showToast('支付成功');
    let carts = wx.getStorageSync('carts');
    carts = carts.filter(v => !v.is_select);

    wx.setStorageSync('carts', carts);
      
    setTimeout(() => {
      wx.navigateBack();
    }, 500);
      
  }
})